﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StartToolWpfApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public class Form
        {
            public string ID { get; set; }
            public string PID { get; set; }
            public string StartTime { get; set; }

            public Form()
            {
            }

            public Form(string ID, string PID, string StartTime)
            {
                this.ID = ID;
                this.PID = PID;
                this.StartTime = StartTime;
            }
        }

        List<Form> formList = new List<Form>();
        List<Process> processList = new List<Process>();
        public string filePath = "";//文件路径
        public string fileName = "";//文件名

        //选择文件并将文件名保存至输入框1
        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;//该值确定是否可以选择多个文件
            dialog.ShowDialog();
            TextBox1.Text = dialog.SafeFileName;
            if (TextBox1.Text.Length != 0)
            {
                filePath = dialog.FileName;
                fileName = dialog.SafeFileName;
            }
        }

        //运行输入框2数量的输入框1中的文件，写入列表并刷新列表
        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            int num = 0;

            if (TextBox2.Text.Length != 0)
            {
                num = Convert.ToInt32(TextBox2.Text.ToString());
                TextBox2.Clear();
            }
            
            for (int i = 0; i < num; i++)
            {
                int index = 0;
                foreach (Process p in processList)
                {
                    if (index == Convert.ToInt32(p.StartInfo.Arguments))
                    {
                        index++;
                    }
                }
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.FileName = filePath;
                process.StartInfo.CreateNoWindow = true;//不显示程序窗口
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//设置DOS窗口不显示
                process.StartInfo.Arguments = index.ToString();
                process.Start();
                processList.Add(process);
                processList = processList.OrderBy(p => Convert.ToInt32(p.StartInfo.Arguments)).ToList();

                List<string> IDList = new List<string>();
                foreach (Form form in formList)
                {
                    IDList.Add(form.ID);
                }

                if (!IDList.Contains(process.StartInfo.Arguments))
                {
                    formList.Add(new Form(process.StartInfo.Arguments, process.Id.ToString(), process.StartTime.ToString()));
                }
                else
                {
                    foreach (Form form in formList)
                    {
                        if (form.ID == process.StartInfo.Arguments)
                        {
                            form.PID = process.Id.ToString();
                            form.StartTime = process.StartTime.ToString();
                        }
                    }
                }
            }
            ListView1.ItemsSource = formList;
            ListView1.Items.Refresh();
        }

        private void ListView1_Select(object sender, SelectionChangedEventArgs e)
        {
            if (ListView1.SelectedItem != null)
            {
                Form selectItem = (Form)ListView1.SelectedItem;
                TextBox3.Text = selectItem.PID;
            }
            else
            {
                TextBox3.Text = null;
            }
        }

        private void ListView1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (ListView1.SelectedItem != null)
            {
                Form selectItem = (Form)ListView1.SelectedItem;
                string ID = selectItem.ID;
                if (selectItem.PID == "")
                {
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.FileName = filePath;
                    process.StartInfo.CreateNoWindow = true;//不显示程序窗口
                    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//设置DOS窗口不显示
                    process.StartInfo.Arguments = ID;
                    process.Start();
                    processList.Add(process);
                    processList = processList.OrderBy(p => p.StartInfo.Arguments).ToList();

                    foreach (Form form in formList)
                    {
                        if (form.ID == process.StartInfo.Arguments)
                        {
                            form.PID = process.Id.ToString();
                            form.StartTime = process.StartTime.ToString();
                        }
                    }
                    ListView1.ItemsSource = formList;
                    ListView1.Items.Refresh();
                }
            }
        }

        //选择列表中的某一行，显示在输入框3，点击停止关闭
        private void Button3_Click(object sender, RoutedEventArgs e)
        {
            if (TextBox3.Text.Length != 0)
            {
                int pid = Convert.ToInt32(TextBox3.Text.ToString());
                Process processNeedRemove = new Process();
                foreach (Process p in processList)
                {
                    if (p.Id == pid)
                    {
                        p.Kill();
                        processNeedRemove = p;
                    }
                }
                foreach (Form form in formList)
                {
                    if (form.ID == processNeedRemove.StartInfo.Arguments)
                    {
                        form.PID = "";
                        form.StartTime = "";
                    }
                }
                processList.Remove(processNeedRemove);
                TextBox3.Clear();
                ListView1.ItemsSource = formList;
                ListView1.Items.Refresh();
            }
        }

        private void Button4_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();
                foreach (Process myProcess in myProcesses)
                {
                    if (myProcess.ProcessName == fileName.Replace(".exe", string.Empty))
                    {
                        myProcess.Kill();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Kill Process Error:" + ex.Message);
            }
            /*            foreach (Process p in processList)
                        {
                            p.Kill();
                        }*/
            processList = new List<Process>();
            formList = new List<Form>();
            ListView1.ItemsSource = formList;
            ListView1.Items.Refresh();
        }
    }
}
